﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace SpeedTest {
    class Program {


        static void Main(string[] args) {
            Program p = new Program();
            try {
                p.ListTime();
                //p.SortedDictionary();
                //p.Dictionary();
            }
            catch (OutOfMemoryException) {

                Console.Error.WriteLine("Out of memory");
                Console.ReadKey();
            }

        }

        public void ListTime() {
            System.IO.StreamWriter streamW = new System.IO.StreamWriter("outputSnack.txt");
            Stopwatch sw = new Stopwatch();
            List<int> things = new List<int>();

            sw.Start();
            for (int i = 0; i < 1000000000; i++) {
                things.Add(i);
                if (i % 10000 == 0) {
                    streamW.WriteLine(ShowTime(sw.Elapsed) + "," + things.Count);
                    // Get the elapsed time as a TimeSpan value.
                    //Console.WriteLine("Runtime: " + ShowTime(sw.Elapsed) + " Size: " + things.Count);
                }
            }
            streamW.Close();
        }

        public void SortedDictionary() {
            Stopwatch sw = new Stopwatch();
            SortedDictionary<int,int> things = new SortedDictionary<int,int>();

            sw.Start();
            for (int i = 0; i < 1000000000; i++) {
                things.Add(i,i);
                if (i % 10000 == 0) {
                    // Get the elapsed time as a TimeSpan value.
                    Console.WriteLine("Runtime: " + ShowTime(sw.Elapsed) + " Size: " + things.Count);
                }
            }
        }

        public void Dictionary() {
            Stopwatch sw = new Stopwatch();
            Dictionary<int, int> things = new Dictionary<int, int>();

            sw.Start();
            for (int i = 0; i < 1000000000; i++) {
                things.Add(i,i);
                if (i % 10000000 == 0) {
                    // Get the elapsed time as a TimeSpan value.
                    Console.WriteLine("Runtime: " + ShowTime(sw.Elapsed) + " Size: " + things.Count);
                }
            }
        }

        private string ShowTime(TimeSpan ts) {
            // Format and display the TimeSpan value.
            string elapsedTime = String.Format("{0:00}.{1:000}", ts.Seconds, ts.Milliseconds);
            return elapsedTime;
        }
    }
}
